#include <iostream>
#include <vector>
#include <cassert>
using namespace std;

// https://leetcode.cn/problems/letter-combinations-of-a-phone-number/
// 17. 电话号码的字母组合
// 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
class Solution {
private:
    const string letterMap[10] = {
            " ",    //0
            "",     //1
            "abc",  //2
            "def",  //3
            "ghi",  //4
            "jkl",  //5
            "mno",  //6
            "pqrs", //7
            "tuv",  //8
            "wxyz"  //9
    };
    vector<string> res;
    // s中保存了此时从 digits[0...index-1]翻译得到的一个字母字符串
    // 寻找和digits[index]匹配的字母，获得digits[0...index]翻译得到的解
    void findCombination(const string &digits, int index, const string &s) {
        cout << index << " : " << s << endl;
        if(index == digits.size()) {
            // 保存 s
            res.push_back(s);
            cout<<"get " << s <<" , return" << endl;
            return;
        }
        char c = digits[index];
        assert( c >= '0' && c <= '9' && c != '1');
        string letters = letterMap[c - '0'];
        for(int i = 0; i < letters.size(); i++) {
            cout << "digits["<<index<<"] = " << c <<", use " << letters[i] <<endl;
            findCombination(digits, index + 1, s + letters[i]);
        }
        cout << "digits["<<index<<"] = " << c <<" completed, return " <<endl;
    }
public:
    vector<string> letterCombinations(string digits) {
        res.clear();
        if(digits == "")
            return res;

        findCombination(digits, 0, "");

        return res;
    }
};


int main() {
    vector<string> res = Solution().letterCombinations("234");
    for(int i = 0; i < res.size(); i++)
        cout << res[i] << endl;
    return 0;
}
